package com.item.mall.service.impl;

import com.item.mall.entity.User;
import com.item.mall.mapper.UserMapper;
import com.item.mall.service.UserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.UUID;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 张津瑞、彭家博
 * @since 2021-07-31
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
    @Autowired
    UserMapper userMapper;


    @Override
    public void updateFileName(MultipartFile file, Integer userId) {

        String fileName = file.getOriginalFilename(); // 文件名
        String suffixName = fileName.substring(fileName.lastIndexOf(".")); // 后缀名
        fileName = UUID.randomUUID() + suffixName; // 新文件名

        //获取存取文件夹相对路径
        File directory = new File("src/main/resources");
        String reportPath = null;
        try {
            reportPath = directory.getCanonicalPath();
            String resource =reportPath + "/static/images/profilePhoto/";
            File dest = new File(resource + fileName);
            file.transferTo(dest);
        } catch (IOException e) {
            e.printStackTrace();
        }
        User user=new User();
        user.setId(userId);
        user.setFileName(fileName);
        userMapper.updateById(user);
    }
}
